package com.iot.framework.rabbitmq.service;


import com.alibaba.fastjson2.JSONObject;
import com.iot.common.enums.DeviceConnectType;
import com.iot.common.enums.DeviceStatusRecordType;
import com.iot.common.utils.DateUtils;
import com.iot.system.domain.DeviceDetails;
import com.iot.system.domain.DeviceState;
import com.iot.system.domain.DeviceStateLogs;
import com.iot.system.service.IDeviceDetailsService;
import com.iot.system.service.IDeviceStateLogsService;
import com.iot.system.service.IDeviceStateService;
import com.iot.system.vo.DeviceStateVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

@Component
@Slf4j
public class DeviceStateService {

    @Resource
    private IDeviceDetailsService iDeviceDetailsService;

    @Resource
    private IDeviceStateService iDeviceStateService;

    @Resource
    private IDeviceStateLogsService iDeviceStateLogsService;



    //判断消息类型 当前消息属于设备状态  分为设备上线 设备下线
    public void deviceState(String message) {
        JSONObject jsonObject = JSONObject.parseObject(message);
        log.info("DeviceStateService_deviceState_设备状态转换完成:{}",jsonObject);
        DeviceStateVo deviceStateVo = jsonObject.toJavaObject(DeviceStateVo.class);
        //获取设备详情
        DeviceDetails deviceDetails = iDeviceDetailsService.selectDeviceDetailsByToken(deviceStateVo.getCredentials());
        if(Objects.isNull(deviceDetails)){
            return;
        }
        processingDeviceState(deviceStateVo, deviceDetails);
        //设备状态更新记录
        processingDeviceStateLogs(deviceStateVo, deviceDetails);
    }

    private void processingDeviceStateLogs(DeviceStateVo deviceStateVo, DeviceDetails deviceDetails) {
        if (Objects.equals(deviceStateVo.getType(), DeviceConnectType.CONNECT.getNumber())) {
            DeviceStateLogs deviceStateLogs = new DeviceStateLogs();
            deviceStateLogs.setDeviceId(deviceDetails.getId());
            //deviceStateLogs.setDeviceShutDown(DateUtils.getDateByTimesTamp(deviceStateVo.getLastDisconnectTime()));
            deviceStateLogs.setDeviceBootUp(DateUtils.getDateByTimesTamp(deviceStateVo.getLastConnectTime()));
            deviceStateLogs.setDeviceState(DeviceStatusRecordType.STATE.getNumber());
            deviceStateLogs.setCreateTime(new Date());
            deviceStateLogs.setUpdateTime(new Date());
            deviceStateLogs.setCreateBy("10101010");
            deviceStateLogs.setUpdateBy("10101010");
            deviceStateLogs.setIsDeleted(0);
            iDeviceStateLogsService.insertDeviceStateLogs(deviceStateLogs);
        }
        if (Objects.equals(deviceStateVo.getType(), DeviceConnectType.DIS_CONNECT.getNumber())) {
            //如果是设备断开连接 则查询最近一次的连接
            DeviceStateLogs deviceStateLogs = iDeviceStateLogsService.selectDeviceStateLogsByDeviceId(deviceDetails.getId(),DeviceStatusRecordType.STATE.getNumber());
            if (Objects.isNull(deviceStateLogs)||Objects.isNull(deviceStateLogs.getId())){
                DeviceStateLogs stateLogs = new DeviceStateLogs();
                stateLogs.setDeviceId(deviceDetails.getId());
                stateLogs.setDeviceShutDown(DateUtils.getDateByTimesTamp(deviceStateVo.getLastDisconnectTime()));
                stateLogs.setDeviceBootUp(DateUtils.getDateByTimesTamp(deviceStateVo.getLastConnectTime()));
                stateLogs.setDeviceState(DeviceStatusRecordType.STATE.getNumber());
                stateLogs.setCreateTime(new Date());
                stateLogs.setUpdateTime(new Date());
                stateLogs.setCreateBy("10101010");
                stateLogs.setUpdateBy("10101010");
                stateLogs.setIsDeleted(0);
                iDeviceStateLogsService.insertDeviceStateLogs(stateLogs);
            }else {
                deviceStateLogs.setDeviceShutDown(DateUtils.getDateByTimesTamp(deviceStateVo.getLastDisconnectTime()));
                iDeviceStateLogsService.updateDeviceStateLogs(deviceStateLogs);
            }
        }
    }

    //设备状态处理
    private void processingDeviceState(DeviceStateVo deviceStateVo, DeviceDetails deviceDetails) {
        log.info("DeviceStateService_processingDeviceState_设备状态处理开始:{}",deviceStateVo);
        DeviceState deviceState = iDeviceStateService.getDeviceStateByDeviceId(deviceDetails.getId());
        if (Objects.isNull(deviceState.getId())) {
            //判断当前消息设备类型
            if (Objects.equals(deviceStateVo.getType(), DeviceConnectType.CONNECT.getNumber())) {
                //设备上线
                deviceState.setStateType(1);
                Date now = new Date(Long.parseLong(deviceStateVo.getLastConnectTime()));
                deviceState.setStateTime(now);
            }
            if (Objects.equals(deviceStateVo.getType(), DeviceConnectType.DIS_CONNECT.getNumber())) {
                //设备下线
                deviceState.setStateType(2);
                Date now = new Date(Long.parseLong(deviceStateVo.getLastDisconnectTime()));
                deviceState.setStateTime(now);
            }
            deviceState.setDeviceId(deviceDetails.getId());
            deviceState.setCreateTime(new Date());
            deviceState.setUpdateTime(new Date());
            deviceState.setCreateBy("10101010");
            deviceState.setUpdateBy("10101010");
            deviceState.setIsDeleted(0);
            iDeviceStateService.insertDeviceState(deviceState);
            log.info("DeviceStateService_processingDeviceState_设备状态为空处理结束:{}",deviceStateVo);
            return;
        }

        log.info("DeviceStateService_processingDeviceState_设备状态不为空处理开始:{}",deviceStateVo);
        if (Objects.nonNull(deviceState.getId())) {
            if (Objects.equals(deviceStateVo.getType(), DeviceConnectType.CONNECT.getNumber())) {
                deviceState.setStateType(1);
                Date now = new Date(Long.parseLong(deviceStateVo.getLastConnectTime()));
                deviceState.setStateTime(now);
            }
            if (Objects.equals(deviceStateVo.getType(), DeviceConnectType.DIS_CONNECT.getNumber())) {
                deviceState.setStateType(2);
                Date now = new Date(Long.parseLong(deviceStateVo.getLastDisconnectTime()));
                deviceState.setStateTime(now);
            }
            deviceState.setUpdateTime(new Date());
            deviceState.setUpdateBy("10101010");
            iDeviceStateService.updateDeviceState(deviceState);
            log.info("DeviceStateService_processingDeviceState_设备状态不为空处理结束:{}",deviceState);
            return;
        }
    }
}
